<template>
  <view class="car-warp">
    <!-- #ifdef H5 -->
    <van-nav-bar @click-left="onClickLeft" left-arrow title="车辆详情"></van-nav-bar>
    <!-- #endif -->
    <scroll-view scroll-y="true" class="" :style="styleHeight">
      <!-- 信息编辑 -->
      <view class="content">
        <text class="title text-bold">基本信息</text>
        <view class="item">
          <view class="itemname">
            <text class="red">*</text>
            车牌号
          </view>
          <view class="itemname-detail">
            <input
              class="user-input"
              type="text"
              v-model="form.plateNumber"
              maxlength="30"
              placeholder="请输入车牌号"
              placeholder-style="color:#545454;font-size:14px" />
          </view>
        </view>
        <view class="line"></view>
        <view class="item">
          <view class="itemname">所有人</view>
          <view class="itemname-detail">
            <input
              class="user-input"
              type="text"
              v-model="form.owner"
              maxlength="30"
              placeholder="请输入所有人"
              placeholder-style="color:#545454;font-size:14px" />
          </view>
        </view>
        <view class="line"></view>
        <view class="item">
          <view class="itemname">车辆类型</view>
          <view class="itemname-detail">
            <input
              class="user-input"
              type="text"
              v-model="form.type"
              maxlength="30"
              placeholder="请输入车辆类型"
              placeholder-style="color:#545454;font-size:14px" />
          </view>
        </view>
        <view class="line"></view>
        <view class="item">
          <view class="itemname">品牌型号</view>
          <view class="itemname-detail">
            <input
              class="user-input"
              type="text"
              v-model="form.model"
              maxlength="30"
              placeholder="示例：丰田牌GIM7250"
              placeholder-style="color:#545454;font-size:14px" />
          </view>
        </view>
        <view class="line"></view>
        <view class="item">
          <view class="itemname">
            <text class="red">*</text>
            车辆识别代码
          </view>
          <view class="itemname-detail">
            <input
              class="user-input"
              type="text"
              v-model="form.vin"
              maxlength="30"
              placeholder="请输入车辆识别代码"
              placeholder-style="color:#545454;font-size:14px" />
          </view>
        </view>
        <view class="line"></view>
      </view>
      <view class="addcar-content">
        <text class="title text-bold">行驶证正本</text>
        <view class="addcar-image">
          <image class="q-image-wrap" :src="form.drivingLicenseFront" mode="scaleToFill"></image>
        </view>
        <text class="title text-bold">行驶证副页</text>
        <view class="addcar-image">
          <image class="q-image-wrap" :src="form.drivingLicenseAfter" mode="scaleToFill"></image>
        </view>
      </view>
      <!-- </view> -->
    </scroll-view>
    <view class="bottom-wrap" :style="styleBntH">
      <button class="bnt-wrap bnt" @tap="submit">保存</button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { getSystemInfo, getUserId, setToastInstance, systemInfo } from '@mixins/info';
import { addCar, checkCarRepeat } from '@/service/apis/mine';
import { getItem } from '@/utils/util';
// #ifdef MP-WEIXIN
import { useToast } from '@/components/wxToast';
setToastInstance(useToast());
// #endif

// 分享
onShareAppMessage(() => {
  return {
    title: '',
    path: '/pages/stationMap/stationMap',
  };
});

const $toast: any = inject('$Toast');

const styleBntH = `height: calc(124rpx + ${getSystemInfo().safeBottom}px)`;
let styleHeight = ref(`height: calc(100vh - (124rpx + ${getSystemInfo().safeBottom}px))`);
// #ifdef H5
styleHeight.value = `height: calc(100vh - (124rpx + ${getSystemInfo().safeBottom}px) - ${
  getSystemInfo().navHeight
}px)`;
// #endif
const form = reactive({
  plateNumber: '',
  owner: '',
  type: '',
  model: '',
  vin: '',
  drivingLicenseFront: '',
  drivingLicenseAfter: '',
  isDefault: true,
});

// const data = reactive({
//   drivingLicenseFront: null,
//   drivingLicenseAfter: null,
// });

onLoad((option: any) => {
  console.log('option=====', option);
  console.log('option.data=====', JSON.parse(option.data));
  const carData = JSON.parse(option.data);
  const temdataBack = option.temdataBack;
  form.plateNumber = carData.number;
  form.owner = carData.owner;
  form.type = carData.type;
  form.model = carData.brand;
  form.vin = carData.vin;
  form.drivingLicenseFront = carData.url;
  form.drivingLicenseAfter = temdataBack;
});

const submit = () => {
  if (!form.plateNumber) {
    $toast('请输入车牌号');
    return;
  }
  if (!form.vin) {
    $toast('请输入车辆识别代码');
    return;
  }
  checkCarRepeat(form, { account: { accountId: getUserId() } }).then((res: any) => {
    if (res) {
      addCar(form, { account: { accountId: getItem('userid') } }).then((res: any) => {
        console.log(res);
        if (res) {
          uni.navigateBack({
            delta: 2,
          });
        }
      });
    } else {
      uni.showModal({
        content: `当前车辆已被其他用户绑定，您的操作会将前一个用户的车辆解绑，是否确认您的信息无误并提交？`,
        showCancel: true,
        success: res => {
          if (res.confirm) {
            addCar(form, { account: { accountId: getItem('userid') } }).then((res: any) => {
              console.log(res);
              if (res) {
                uni.navigateBack({
                  delta: 2,
                });
              }
            });
          } else {
            console.log('cancel'); //点击取消之后执行的代码
          }
        },
      });
    }
  });
};

// #ifdef H5
const onClickLeft = () => {
  uni.navigateBack();
};
setTimeout(() => {
  const navBarEle = document.querySelector('.van-nav-bar') as HTMLElement;
  navBarEle.style.paddingTop = `${systemInfo.statusBarHeight}px`;
});
// #endif
</script>
<style lang="scss">
page {
  background-color: #f4f4f4;
}
</style>
<style lang="scss" scoped>
.car-warp {
  padding: 12px 0;

  .content {
    background-color: #ffffff;
    border-radius: 8px;
    padding: 0 16px;

    .title {
      font-size: 14px;
      line-height: 24px;
      color: #000;
      margin: 12px 0 8px 0;
    }

    .item {
      display: flex;
      justify-content: space-between;
      margin: 0 auto;
      height: 56px;
      line-height: 56px;
      align-items: center;

      .itemname {
        font-size: 16px;
        color: #242424;
        white-space: nowrap;
      }

      .itemname-detail {
        margin-left: 15px;
        // width: calc(100% - 108px);
        color: #545454;
        text-align: right;
      }

      .red {
        color: $uni-color-error;
      }
    }
  }

  .user-input {
    // width: 60vw;
    height: 15vw;
    font-size: 16px;
    color: #000000;
  }

  .line {
    border-bottom: 1px solid #f2f2f2;
    height: 1px;
    position: relative;
  }

  .addcar-content {
    // margin: 0 16px 12px 16px;
    // padding: 16px;
    margin-top: 12px;
    background-color: #fff;
    font-size: 14px;
    color: #545454;
    border-radius: 8px;
    // background-color: #ffffff;
    // border-radius: 8px;
    padding: 12px 16px;

    .title {
      font-size: 14px;
      line-height: 22px;
      color: #858585;
      margin: 12px 0 8px 0;
    }

    &:last-child {
      border: 0;
    }

    .title {
      line-height: 24px;
      color: #000;
    }

    .info {
      display: flex;
      font-size: 14px;
      justify-content: space-between;

      .left {
        width: 30%;
        color: #858585;
      }

      .right {
        width: 70%;
        text-align: right;
        color: #242424;
      }

      .test {
        line-height: 22px;
        margin-top: 8px;

        .content-input {
          height: 25px;
        }
      }

      .test1 {
        line-height: 22px;
        margin-top: 8px;
        color: #545454;
      }

      .text-red {
        color: #fa3939;
      }
    }

    .addcar-image {
      padding: 8px 0 16px 0;

      .q-image-wrap {
        position: relative;
        width: 317px;
        height: 179px;
      }
    }

    .line {
      margin: 12px 0;
      height: 1px;
      background-color: #f2f2f2;
      // border-bottom: 1px solid #f2f2f2;
    }
  }

  .bottom-wrap {
    display: flex;
    position: fixed;
    width: 100%;
    bottom: 0;
    background: #fff;
    border-top: 1px solid #f2f2f2;
    box-sizing: border-box;

    .bnt {
      height: 44px;
      width: 343px;
      margin-top: 9px;
      border-radius: 50px;
    }
  }
}
</style>
